package com.ydc.controller;


import com.ydc.pojo.PageResult;
import com.ydc.pojo.User;
import com.ydc.service.UserService;
import com.ydc.vo.SysResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/user")
@Api(tags = "登陆管理")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/one/{id}")
    public SysResult one(@PathVariable Integer id){
        User user = userService.selectOne(id);
        return SysResult.success(user);
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public SysResult login(@RequestBody User user) {
        Map<String, Object> map = userService.selectUser(user);
        if (map.containsKey("id"))
            return SysResult.success(map);
        return SysResult.fail();
    }

    @ApiOperation("分页查询")
    @GetMapping("/list")
    public SysResult findAll(PageResult pageResult) {
        PageResult result = userService.findAll(pageResult);
        return SysResult.success(result);

    }

    @ApiOperation("添加用户")
    @PostMapping("/addUser")
    public SysResult addUser(@RequestBody User user) {
        userService.addUser(user);
        return SysResult.success();

    }


    @ApiOperation("修改用户信息")
    @PutMapping("/updateUser")
    public SysResult updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return SysResult.success();

    }

    @ApiOperation("删除用户")
    @DeleteMapping("/deleteUser/{id}")
    public SysResult deleteUser(@PathVariable Integer id) {
        userService.deleteUser(id);
        return SysResult.success();
    }

}





















